package com.example.administrator.x2picturetransliteration.Home.Activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.x2picturetransliteration.Base.BaseActivity;
import com.example.administrator.x2picturetransliteration.R;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 协议
 * Created by Administrator on 2019/8/27.
 */

public class AgreementActivity extends BaseActivity{

    public static void startAgreementActivity(Context context,String title,String content){

        Intent intent = new Intent(context,AgreementActivity.class);
        intent.putExtra("title",title);
        intent.putExtra("content",content);
        context.startActivity(intent);

    }

    @BindView(R.id.TitleText)TextView TitleText;
    @BindView(R.id.LButton)ImageView LButton;

    @BindView(R.id.webview)WebView webview;

    @BindView(R.id.sview)View sview;

    @BindView(R.id.text1)TextView text1;

    @Override
    public int intiLayout() {
        return R.layout.home_activity_agreement;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TitleText.setText(getIntent().getStringExtra("title"));
        LButton.setImageResource(R.mipmap.home_backw);

        if (getIntent().getStringExtra("content").indexOf("http")==-1 && getIntent().getStringExtra("content").indexOf("<a>")==-1){

            sview.setVisibility(View.VISIBLE);
            webview.setVisibility(View.GONE);

            text1.setText(getIntent().getStringExtra("content"));

        }else if (getIntent().getStringExtra("content").indexOf("http")==-1){

            webview.setVisibility(View.VISIBLE);
            sview.setVisibility(View.GONE);

            webview.loadDataWithBaseURL(null, "<head><style>img{max-width:100% !important;}</style></head>" + getIntent().getStringExtra("content") , "text/html", "UTF-8", null);

        }else {

            webview.setVisibility(View.VISIBLE);
            sview.setVisibility(View.GONE);

            webview.loadUrl(getIntent().getStringExtra("content"));
        }


        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setAppCacheEnabled(true);
        //设置 缓存模式
        webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        // 开启 DOM storage API 功能
        webview.getSettings().setDomStorageEnabled(true);

        webview.setWebViewClient(new WebViewClient(){

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed(); //解决加载https报错问题
            }
        });

        webview.setDownloadListener(new DownloadListener() {
            @Override
            public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) {
                // H5中包含下载链接的话让外部浏览器去处理
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.addCategory(Intent.CATEGORY_BROWSABLE);
                intent.setData(Uri.parse(url));
                startActivity(intent);
            }
        });

    }

    @OnClick(R.id.left_button)
    public void left_buttonClick(){

        finish();
    }
}
